from flask import  Flask,render_template,request,redirect,url_for
import  os
import  sqlite3

app = Flask(__name__)

#数据库连接

DATABASE = 'FishModel.db'

def get_db_connection():
    conn = sqlite3.connect(DATABASE)
    conn.row_factory = sqlite3.Row
    return conn


# # 创建数据表
# def init_db():
#     conn = get_db_connection()
#     c = conn.cursor()
#     c.execute('''CREATE TABLE IF NOT EXISTS DATA
#                  (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, path TEXT NOT NULL, date TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP)''')
#     c.execute('''CREATE TABLE IF NOT EXISTS Model
#                  (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER, path TEXT NOT NULL, num INTEGER, date TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP)''')
#     conn.commit()
#     conn.close()
#
# init_db()

# 用于文件上传与提示
@app.route('/',methods = ['GET','POST'])
def index():
    if request.method == 'POST':
        #处理文件上传
        file = request.files['file']
        file_path = os.path.join('uploads',file.filename)
        file.save(file_path)

        # 将文件信息保存到数据库
        conn = get_db_connection()
        c = conn.cursor()
        c.execute("INSERT INTO DATA (name,path) VALUES (?,?)",(file.filename,file_path))
        conn.commit()
        # conn.close()

        conn = get_db_connection()
        data = conn.execute('SELECT * FROM DATA').fetchall()

        return render_template('test555.html',data=data)

    if __name__ == '__main__':
        app.run(debug=True)